#include <iostream>
#include <unistd.h>
//string.h
#include <cstring>
//errno.h
#include <cerrno>


//父进程进行读取
void FatherProcessRead(int rfd)
{
    char inbuffer[size];
    while(true)
    {
        //sleep
        ssize_t n= read(rfd,inbuffer,sizeof(inbuffer)-1);
        if(n>0)
        {
            inbuffer[n]=0;
            std::cout << "father get massage:"<<inbuffer<<std::ebdl;

        }
        //如果是read的返回值是0，表示写端直接关闭了，我们读到了文件的结尾
        else if(n == 0)
        {
            //如果read的返回值是0，表示写端直接关闭了，我们读到了文件的结尾
            std::cout << "father get return val:"<<n<< std::endl;
            break;
        }
        else if(n<0)
        {
            std::cerr <<"read error"<< std::endl;
            break;
        }
       

    }
}



int main()
{
        //1.创建管道
        int pipefd[2];
        int n = pipe(pipefd);//输出型参数。 rfd, wfd
        if(n!=0)
        {
            std::cerr<< "errno:"<<errno<<":"<<"errstring:"<<strerror(errno)<<std::endl;
            return 1;
        }
        
        std::cout << "pipefd[0]:"<<pipefd[0]<< ", pipefd[1]:"<<pipefd[1]<<std::endl;

        //2.创建子进程
        pid_t id = fork();
        if(id==0)
        {
            //子进程
            //3.关闭不需要的fd
            close(pipefd[0]);
            exit(0);
        }
        
        //父进程 --- read
        //3.关闭不需要的fd
        close(pipefd[1]);

        return 0;
}